//
//  UIDevice+LYAdd.m
//  LYDevelopKit
//
//  Created by 李阳 on 2018/5/25.
//

#import "UIDevice+LYAdd.h"
#import <sys/utsname.h>


@interface LYDevice ()

@end
@implementation LYDevice

+ (NSDictionary*)deviceNamesByCode
{
    static NSDictionary *deviceNamesByCode = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        deviceNamesByCode = @{
                              //iPhones
                              @"iPhone3,1"  : @(iPhone4),
                              @"iPhone3,2"  : @(iPhone4),
                              @"iPhone3,3"  : @(iPhone4),
                              @"iPhone4,1"  : @(iPhone4S),
                              @"iPhone4,2"  : @(iPhone4S),
                              @"iPhone4,3"  : @(iPhone4S),
                              @"iPhone5,1"  : @(iPhone5),
                              @"iPhone5,2"  : @(iPhone5),
                              @"iPhone5,3"  : @(iPhone5C),
                              @"iPhone5,4"  : @(iPhone5C),
                              @"iPhone6,1"  : @(iPhone5S),
                              @"iPhone6,2"  : @(iPhone5S),
                              @"iPhone7,2"  : @(iPhone6),
                              @"iPhone7,1"  : @(iPhone6Plus),
                              @"iPhone8,1"  : @(iPhone6S),
                              @"iPhone8,2"  : @(iPhone6SPlus),
                              @"iPhone8,4"  : @(iPhoneSE),
                              @"iPhone9,1"  : @(iPhone7),
                              @"iPhone9,3"  : @(iPhone7),
                              @"iPhone9,2"  : @(iPhone7Plus),
                              @"iPhone9,4"  : @(iPhone7Plus),
                              @"iPhone10,1" : @(iPhone8),
                              @"iPhone10,4" : @(iPhone8),
                              @"iPhone10,2" : @(iPhone8Plus),
                              @"iPhone10,5" : @(iPhone8Plus),
                              @"iPhone10,3" : @(iPhoneX),
                              @"iPhone10,6" : @(iPhoneX),
                              @"i386"       : @(Simulator),
                              @"x86_64"     : @(Simulator),
                              
                              //iPads
                              @"iPad1,1"  : @(iPad1),
                              @"iPad2,1"  : @(iPad2),
                              @"iPad2,2"  : @(iPad2),
                              @"iPad2,3"  : @(iPad2),
                              @"iPad2,4"  : @(iPad2),
                              @"iPad2,5"  : @(iPadMini),
                              @"iPad2,6"  : @(iPadMini),
                              @"iPad2,7"  : @(iPadMini),
                              @"iPad3,1"  : @(iPad3),
                              @"iPad3,2"  : @(iPad3),
                              @"iPad3,3"  : @(iPad3),
                              @"iPad3,4"  : @(iPad4),
                              @"iPad3,5"  : @(iPad4),
                              @"iPad3,6"  : @(iPad4),
                              @"iPad4,1"  : @(iPadAir),
                              @"iPad4,2"  : @(iPadAir),
                              @"iPad4,3"  : @(iPadAir),
                              @"iPad4,4"  : @(iPadMini2),
                              @"iPad4,5"  : @(iPadMini2),
                              @"iPad4,6"  : @(iPadMini2),
                              @"iPad4,7"  : @(iPadMini3),
                              @"iPad4,8"  : @(iPadMini3),
                              @"iPad4,9"  : @(iPadMini3),
                              @"iPad5,1"  : @(iPadMini4),
                              @"iPad5,2"  : @(iPadMini4),
                              @"iPad5,3"  : @(iPadAir2),
                              @"iPad5,4"  : @(iPadAir2),
                              @"iPad6,3"  : @(iPadPro9Dot7Inch),
                              @"iPad6,4"  : @(iPadPro9Dot7Inch),
                              @"iPad6,7"  : @(iPadPro12Dot9Inch),
                              @"iPad6,8"  : @(iPadPro12Dot9Inch),
                              @"iPad6,11" : @(iPad5),
                              @"iPad6,12" : @(iPad5),
                              @"iPad7,1"  : @(iPadPro12Dot9Inch2Gen),
                              @"iPad7,2"  : @(iPadPro12Dot9Inch2Gen),
                              @"iPad7,3"  : @(iPadPro10Dot5Inch),
                              @"iPad7,4"  : @(iPadPro10Dot5Inch),
                              
                              //iPods
                              @"iPod1,1" : @(iPodTouch1Gen),
                              @"iPod2,1" : @(iPodTouch2Gen),
                              @"iPod3,1" : @(iPodTouch3Gen),
                              @"iPod4,1" : @(iPodTouch4Gen),
                              @"iPod5,1" : @(iPodTouch5Gen),
                              @"iPod7,1" : @(iPodTouch6Gen)};
#pragma clang diagnostic pop
    });
    
    return deviceNamesByCode;
}

+ (LYDeviceType)deviceType
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *code = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    LYDeviceType type = (LYDeviceType)[[self.deviceNamesByCode objectForKey:code] unsignedIntegerValue];
    
    return type;
}
+ (BOOL)isIPhone
{
    return ([self deviceType] & iPhone);
}
+ (BOOL)isIPad
{
    return ([self deviceType] & iPad);
}
+ (BOOL)isIPod
{
    return ([self deviceType] & iPod);
}
+ (BOOL)isSimulator
{
    return ([self deviceType] == Simulator);
}


+ (LYDeviceSize)resolutionSize
{
    CGFloat screenHeight = 0;
    
    if ([self versionGreaterThanOrEqualTo:@"8"]) {
        screenHeight = MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
    } else {
        screenHeight = [[UIScreen mainScreen] bounds].size.height;
    }
    
    if (screenHeight == 480) // 320
    {
        return Screen3Dot5inch;
    } else if(screenHeight == 568) // 320
    {
        return Screen4inch;
    } else if(screenHeight == 667) // 375
    {
        return  Screen4Dot7inch;
    } else if(screenHeight == 736) // 414
    {
        return Screen5Dot5inch;
    } else if (screenHeight == 812) // 375
    {
        return Screen5Dot8inch;
    } else
    {
        return UnknownSize;
    }
}
+ (LYDeviceSize)deviceSize
{
    LYDeviceSize size = [self resolutionSize];
    if ([self isZoomed])
    {
        if (size == Screen4inch)
        {
            size = Screen4Dot7inch;
        }
        else if (size == Screen4Dot7inch)
        {
            size = Screen5Dot5inch;
        }
    }
    return size;
}
+ (BOOL)isZoomed
{
    if ([self resolutionSize] == Screen4inch && [UIScreen mainScreen].nativeScale > 2) {
        return YES;
    }else if ([self resolutionSize] == Screen4Dot7inch && [UIScreen mainScreen].scale == 3){
        return YES;
    }
    
    return NO;
}


+ (BOOL)versionEqualTo:(NSString *)version
{
    return ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedSame);
}

+ (BOOL)versionGreaterThan:(NSString *)version
{
    return ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedDescending);
}

+ (BOOL)versionGreaterThanOrEqualTo:(NSString *)version
{
    return ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] != NSOrderedAscending);
}

+ (BOOL)versionLessThan:(NSString *)version
{
    return ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedAscending);
}

+ (BOOL)versionLessThanOrEqualTo:(NSString *)version
{
    return ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] != NSOrderedDescending);
}

@end

@implementation UIDevice (LYAdd)

@end
